package com.ymatou.iapi.trading.sellerquery2.testcase;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.bean.AgentProductBean;
import com.shop2cn.iapi.sqoptrade.resp.TradingServiceResp;
import com.ymatou.iapi.synctrade.service.TradingSyncService;
import com.ymatou.iapi.trading.sellerquery2.parameter.ExportSqOrderInfosReq;
import com.ymatou.iapi.trading.sellerquery2.service.ExistsExportSqOrderInfosCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.business.service.shenqi.DGSQBuildProxyChainCallService;
import com.ymttest.business.service.shenqi.SqTradingCallService;
import com.ymttest.utils.YMTDateUtil;

@TestSuite
public class Ts_ExistsExportSqOrderInfos {

	private static ExportSqOrderInfosReq exportSqOrderInfosReq;
	private static ExistsExportSqOrderInfosCall existsExportSqOrderInfosCall;

	private static long autotest_goodsOwnerId = 500002490;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("是否存在导出的神器订单信息");
	}

	@Before
	public void caseUp() {

		exportSqOrderInfosReq = new ExportSqOrderInfosReq();
		existsExportSqOrderInfosCall = new ExistsExportSqOrderInfosCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(existsExportSqOrderInfosCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_001() {
		Logger.start(true, "店主下单，查询货主是否有对应的订单信息");
		try {

			Date orderStartTime = YMTDateUtil.getDate();

			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			SqTradingCallService.placeOrder(productBean);

			exportSqOrderInfosReq.setSellerId(autotest_goodsOwnerId);
			exportSqOrderInfosReq.setOrderStartTime(orderStartTime);
			exportSqOrderInfosReq.setOrderEndTime(YMTDateUtil.getDate());
			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals(true, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_002() {
		Logger.start(true, "店主下单，查询店主是否有对应的订单信息");
		try {

			Date orderStartTime = YMTDateUtil.getDate();
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, false);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			exportSqOrderInfosReq.setSellerId(sellerId);
			exportSqOrderInfosReq.setOrderStartTime(orderStartTime);
			exportSqOrderInfosReq.setOrderEndTime(YMTDateUtil.getDate());
			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals(true, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_003() {
		Logger.start(true, "代理下单，查询代理是否有对应的订单信息");
		try {

			Date orderStartTime = YMTDateUtil.getDate();
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			exportSqOrderInfosReq.setSellerId(sellerId);
			exportSqOrderInfosReq.setOrderStartTime(orderStartTime);
			exportSqOrderInfosReq.setOrderEndTime(YMTDateUtil.getDate());
			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals(true, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_004() {
		Logger.start(true, "用户无可导出的订单");
		try {

			Date orderStartTime = YMTDateUtil.getDate();
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean);

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			exportSqOrderInfosReq.setSellerId(sellerId + 1);
			exportSqOrderInfosReq.setOrderStartTime(orderStartTime);
			exportSqOrderInfosReq.setOrderEndTime(YMTDateUtil.getDate());
			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals(false, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_005() {
		Logger.start(true, "直销商下单，查询直销商是否有对应的订单信息");
		try {

			Date orderStartTime = YMTDateUtil.getDate();
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, productBean.getSellerId());

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			exportSqOrderInfosReq.setSellerId(sellerId);
			exportSqOrderInfosReq.setOrderStartTime(orderStartTime);
			exportSqOrderInfosReq.setOrderEndTime(YMTDateUtil.getDate());
			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals(true, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_006() {
		Logger.start(true, "直销商发货，查询直销商是否有对应的订单信息");
		try {

			Logger.comment("前置数据准备开始");
			Date orderStartTime = YMTDateUtil.getDate();
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, productBean.getSellerId());

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			Long userId = placeOrderResp.getUserId();
			long orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			BigDecimal totalPrice = placeOrderResp.getPayableAmount();

			// 支付订单
			Long thirdTradingId = SqTradingCallService.payOrder(userId,
					placeOrderResp.getTradingId());

			// 支付网关支付回调交易
			PayGateWayCallService.DGSQNotifyCall(
					String.valueOf(thirdTradingId),
					totalPrice.subtract(BigDecimal.ZERO).toString(),
					userId.intValue(), BigDecimal.ZERO.toPlainString());

			TradingSyncService.getSyncResult(orderId);
			Thread.sleep(3000);

			// 接单
			SqTradingCallService.sellerAcceptOrder(sellerId, orderId);
			TradingSyncService.getSyncResult(orderId);

			Thread.sleep(5000);

			// 神器订单发货
			SqTradingCallService.delivery(sellerId, orderId, 1L, null);
			TradingSyncService.getSyncResult(orderId);

			Logger.comment("前置数据准备结束");

			exportSqOrderInfosReq.setSellerId(sellerId);
			exportSqOrderInfosReq.setOrderStartTime(orderStartTime);
			exportSqOrderInfosReq.setOrderEndTime(YMTDateUtil.getDate());
			exportSqOrderInfosReq.setOrderStates(Arrays.asList(3));
			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals(true, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_007() {
		Logger.start(true, "不传入sellerId");
		try {

			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals("sellerId:商家Id不能为空",
					existsExportSqOrderInfosCall.getMsg(), "验证msg");
			Logger.verifyEquals(false, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_ExistsExportSqOrderInfos_008() {
		Logger.start(true, "下单的物流为999，但是查询的为100");
		try {

			Date orderStartTime = YMTDateUtil.getDate();
			AgentProductBean productBean = DGSQBuildProxyChainCallService
					.createProxychain((int) autotest_goodsOwnerId, 1, true);
			TradingServiceResp placeOrderResp = SqTradingCallService
					.placeOrder(productBean, productBean.getSellerId());

			long sellerId = placeOrderResp.getOrderList().get(0).getSellerId();

			exportSqOrderInfosReq.setSellerId(sellerId);
			exportSqOrderInfosReq.setOrderStartTime(orderStartTime);
			exportSqOrderInfosReq.setOrderEndTime(YMTDateUtil.getDate());
			exportSqOrderInfosReq.setDeliveryType(Arrays.asList(100));
			exportSqOrderInfosReq.setPartitionKey("");

			existsExportSqOrderInfosCall.setData(exportSqOrderInfosReq);
			existsExportSqOrderInfosCall.callService();

			Logger.verifyEquals(false, existsExportSqOrderInfosCall.isExist(),
					"验证exist");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	public static void main(String[] args) {
		Logger.comment(YMTDateUtil.getFormatTime(YMTDateUtil.getDate(), YMTDateUtil.YMDTHMS_STRING));
	}
}